package com.tuozixuan.easyexcel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;

/**
 * 
 * https://www.jianshu.com/p/d1d264c817ef
 *
 */
public class EasyExcelWriteDemo {

	public static void main(String[] args) {
	    List<User> lists = new ArrayList<>();
	    for(int i = 0; i <= 10; i++) {
	    	User user = new User();
	    	user.setId(Long.valueOf(i + 1));
	    	user.setName("李四" + i);
	    	user.setAge(30);
	    	user.setScore(1500.00D);
	    	user.setSalary(new BigDecimal("23415.23454235"));
	    	user.setCreateDate(new Date());
	        lists.add(user);
	    }
	    
	    EasyExcel.write("用戶信息表.xlsx", User.class)
	    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
	    .sheet(0, "用戶信息").doWrite(lists);
	    
	    EasyExcel.write("用戶信息表1.xlsx", User.class)
	    .includeColumnFieldNames(Arrays.asList("id", "name"))
	    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
	    .sheet(0, "用戶信息").doWrite(lists);
	}

}
